package festival.input;

import java.awt.AWTKeyStroke;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public class Alert {
    private InputListener[] mInputListeners;
    private String mTitle;
    private String mMessage;
    private Object[] mOptions;
    
    private int mChoice;
    
    public Alert() {
    }
    
    public Alert(String title, String message, String[] options, InputListener[] actions) {
        mTitle = title;
        mMessage = message + "\n";
        for(int i = 0; i < options.length; i++) {
            mMessage += actions[i].getKeyString() + "  =  " + options[i];
            if(i < options.length - 1) {
                mMessage += "\n";
            }
        }
        mOptions = options;
        mInputListeners = actions;
    }
    
//    private void initListeners(final JComponent dialog) {
//        JRootPane c = dialog.getRootPane();
//        int i = 0;
//        for(final Action a : mActions) {
//            c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
//                    KeyStroke.getKeyStroke(l.getKeyCode(), 0), l.getKeyString());
//            c.getActionMap().put(l.getKeyString(), new AbstractAction() {
//                public void actionPerformed(ActionEvent e) {
//                    l.doAction();
////                    dialog.dispose();
//                }
//            });
//        }
//    }
    
    public void show() {
//        mChoice = JOptionPane.showOptionDialog(mFrame,
//                mMessage,
//                mTitle,
//                JOptionPane.YES_NO_OPTION,
//                JOptionPane.QUESTION_MESSAGE,
//                null,
//                mOptions,
//                mOptions[0]);
        
        JOptionPane optionPane = new JOptionPane(mMessage, JOptionPane.PLAIN_MESSAGE, JOptionPane.CANCEL_OPTION,
                null, mOptions, mOptions[0]);
        final JDialog dialog = optionPane.createDialog("Confirm Dialog");
        dialog.setIconImage(null);
//        initListeners(optionPane);
        
        JRootPane c = optionPane.getRootPane();
        int i = 0;
        for(final InputListener l : mInputListeners) {
            c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                    KeyStroke.getKeyStroke(l.getKeyCode(), 0), mOptions[i]);
            c.getActionMap().put(mOptions[i], new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    l.doAction();
                    dialog.dispose();
                }
            });
            i++;
        }
//        dialog.addKeyListener(new KeyListener() {
//            public void keyPressed(KeyEvent arg0) {
//                System.out.println("fuck");
//            }
//            public void keyReleased(KeyEvent arg0) {
//            }
//            public void keyTyped(KeyEvent arg0) {
//            }
//        });
//        Set<AWTKeyStroke> focusTraversalKeys = new HashSet<AWTKeyStroke>(dialog.getFocusTraversalKeys(0));
//        focusTraversalKeys.add(AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_B, KeyEvent.VK_UNDEFINED));
//        dialog.setFocusTraversalKeys(0, focusTraversalKeys);
        dialog.setVisible(true);
        dialog.requestFocusInWindow();
//        Integer option = optionPane.getValue() != null ? (Integer) optionPane.getValue() : null;
//        mChoice = option;
    }
    
    public int getChoice() {
        return mChoice;
    }
}
